/**
 * gwt-validator 
 *
 * Copyright (C) 2006  Nikolaus Rumm, Christian Simonutti
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package gwt.validator.client.check;

import gwt.validator.client.exception.ValidationIssue;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBoxBase;

/**
 * Validator for checking an Integer's minimum value.
 * 
 * @author <a href="mailto:nikolaus.rumm@gmail.com">Nikolaus Rumm</a>
 */
public class IntegerMinValueCheck extends BaseCheck {

	/**
	 * The minimum valid value of the Integer
	 */
	private int minValue;

	/**
	 * The text box that displays the text to be validated
	 */
	private TextBoxBase textBox;

	/**
	 * The value to validate
	 */
	private Integer value = null;

	/**
	 * Constructor
	 * 
	 * @param aTextBox
	 *            The text box that displays the text to be validated
	 * @param aLabel
	 *            The label that displays the property's name
	 * @param aMinValue
	 *            The minimum valid value of the Integer
	 */
	public IntegerMinValueCheck(TextBoxBase aTextBox, Label aLabel,
			int aMinValue) {
		super(aLabel);

		checkNotNull(aTextBox);
		checkMinValue(aMinValue, 0);

		this.textBox = aTextBox;
		this.minValue = aMinValue;
	}

	/**
	 * Constructor
	 * 
	 * @param aValue
	 *            The value to be validated
	 * @param aLabel
	 *            The label that displays the property's name
	 * @param aMinValue
	 *            The minimum valid value of the Integer
	 */
	public IntegerMinValueCheck(Integer aValue, Label aLabel, int aMinValue) {
		super(aLabel);

		checkNotNull(aValue);
		checkMinValue(aMinValue, 0);

		this.value = aValue;
		this.minValue = aMinValue;
	}

	/**
	 * Constructor
	 * 
	 * @param aTextBox
	 *            The text box that displays the text to be validated
	 * @param aPropertyName
	 *            The property's name
	 * @param aMinValue
	 *            The minimum valid value of the Integer
	 */
	public IntegerMinValueCheck(TextBoxBase aTextBox, String aPropertyName,
			int aMinValue) {
		super(aPropertyName);

		checkNotNull(aTextBox);
		checkMinValue(aMinValue, 0);

		this.textBox = aTextBox;
		this.minValue = aMinValue;
	}

	/**
	 * Constructor
	 * 
	 * @param aValue
	 *            The value to be validated
	 * @param aPropertyName
	 *            The property's name
	 * @param aMinValue
	 *            The minimum valid value of the Integer
	 */
	public IntegerMinValueCheck(Integer aValue, String aPropertyName,
			int aMinValue) {
		super(aPropertyName);

		checkNotNull(aValue);
		checkMinValue(aMinValue, 0);

		this.value = aValue;
		this.minValue = aMinValue;
	}

	public int getMinValue() {
		return minValue;
	}

	/**
	 * Read-accessor to the validated value.
	 * 
	 * @return The value
	 */
	public Integer getValue() {
		Integer res = null;
		if (this.textBox != null) {
			res = Integer.valueOf(this.textBox.getText());
		} else {
			res = this.value;
		}

		if (res == null) {
			throw new NumberFormatException();
		}

		return res;
	}

	public List validateImpl() {
		List res = new ArrayList();

		int value = -1;
		try {
			value = this.getValue().intValue();
		} catch (NumberFormatException e) {
			res.add(new ValidationIssue(this.getIssueMessageFactory()
					.integerWrongSyntax(this.getPropertyName())));
			return res;
		}
		if (value < this.minValue) {
			res.add(new ValidationIssue(this.getIssueMessageFactory()
					.integerTooSmall(this.getPropertyName(), this.minValue)));
		}

		return res;
	}
}
